package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.User;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IUserRegisterService;
import com.cskaoyan.user.utils.NoBlockSendMail;
import lombok.SneakyThrows;
import org.apache.commons.codec.cli.Digest;
import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.cskaoyan.user.constants.UserRetCode.USER_INFOR_UPDATE_FAIL;
import static com.cskaoyan.user.constants.UserRetCode.USER_REGISTER_FAILED;

@Service
public class IUserRegisterServiceImpl implements IUserRegisterService {
    @Autowired
    Redisson redisson;

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    JavaMailSender mailSender;

    @SneakyThrows
    @Override
    @Transactional
    public void register(UserRegisterRequest request) {
        //member表
        Member user = new Member();
        user.setUsername(request.getUserName());
        user.setPassword(DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes()));
        user.setEmail(request.getEmail());
        user.setState(1);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        user.setIsVerified("N");
        try {
            memberMapper.insert(user);
        }catch (Exception e){
            throw new ValidateException(USER_REGISTER_FAILED.getCode(),USER_REGISTER_FAILED.getMessage());
        }
        //验证表
        UserVerify userVerify = new UserVerify();
        userVerify.setRegisterDate(new Date());
        userVerify.setUsername(user.getUsername());
        userVerify.setIsExpire("N");
        String key=user.getUsername()+user.getPassword()+ UUID.randomUUID().toString();
        String uuid= DigestUtils.md5DigestAsHex(key.getBytes());
        userVerify.setUuid(uuid);
        userVerify.setIsVerify("N");
        int verifyInsert = userVerifyMapper.insert(userVerify);
        if (verifyInsert!=1){
            throw new ValidateException(USER_REGISTER_FAILED.getCode(),USER_REGISTER_FAILED.getMessage());
        }


        //发送邮件
        NoBlockSendMail noBlockSendMail = new NoBlockSendMail(mailSender,user.getEmail(), uuid);
        new Thread(noBlockSendMail).start();
    }

    @Override
    public void verify(UserVerifyRequest request) {

    }


    @Transactional
    @Override
    public void userVerify(String uuid) {
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        Example example = new Example(userVerify.getClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uuid",uuid);
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        int code = userVerifyMapper.updateByExampleSelective(userVerify, example);
        if (code!=1){
            throw new ValidateException(USER_INFOR_UPDATE_FAIL.getCode(),USER_INFOR_UPDATE_FAIL.getMessage());
        }
        memberMapper.updateStatus(userVerifies.get(0).getUsername());
    }

    public void sendMail(String email,String uid){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("kerbaluiz@163.com");
        message.setTo(email);
        message.setSubject("csmall商城激活2");
        message.setText("http://localhost:8080/user/verify?uuid="+uid);
        mailSender.send(message);
    }
}
